package com.kwlt.smc.web;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import com.kwlt.smc.util.ConUtils;


public class SmsHttpTest {
	public static void main(String[] args) throws IOException {
		SmsHttpTest mms=new SmsHttpTest();
		//mms.send("13521260152,13366790080");
		//mms.send("15300693506,13810545608,18610019614");
		//mms.send("13810514135,13810514136,13810514137,13810514138,13810514139,13710514135,13810514140,13810514141,13810514142,13810514143,13810514144,13598671215,13598671218");
		mms.send("13810545608,18610545608,13810545609,13810545602,13810545601,13810545607");
	}
	private void send(final String mobileList) throws IOException {
		//http://60.28.200.160:8080/smc/sms/send?user=admin&pwd=admin&dest=18610019614&msg=wowo测试
//		String strUrl = "http://42.121.4.102/smc/sms/dsend/focuscn";
		String strUrl = "http://localhost:8080/smc/sms/send";
//		String strUrl = "http://localhost:8080/smc/sms/dsend/focuscn";
		Map<String, String> paramentMap = new LinkedHashMap<String, String>() {
			{
				put("user", "httptest");
				put("pwd", "httptest");
			/*	put("city","");
				put("business","sms");
				put("type","type2");
				put("send_id","1231231");*/
				//put("sendtime","2013-04-08 23:45:23");
				put("dest", mobileList);
				put("msg", "【测试测试】发条看看"+new Date());
			}
		};
		//long currentTimeMillis = System.currentTimeMillis();
		//System.out.println("simu result:" + );
		//System.out.println(System.currentTimeMillis()-currentTimeMillis);
		String string = sendHttpPostRequest(strUrl, paramentMap);
		System.out.println(string);
	}

	public static String sendHttpPostRequest(String strUrl,
			Map<String, String> paramentMap) {
		URL url;
		HttpURLConnection httpConn;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(strUrl);
			httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setDoOutput(true);
			httpConn.setRequestMethod("POST");
			httpConn.setRequestProperty("content-type",
					"application/x-www-form-urlencoded");
			StringBuffer parament = new StringBuffer();
			for (Map.Entry<String, String> entry : paramentMap.entrySet()) {
				String key=entry.getKey();
				if (key==null || "".equals(key))
					parament.append(URLEncoder.encode(entry.getValue(), "UTF-8")
							+ "&");
				else
					parament.append(key + "="
							+ URLEncoder.encode(entry.getValue(), "UTF-8")
							+ "&");
			}
			if (parament.toString().endsWith("&")) {
				parament.setLength(parament.length() - 1);
			}
			httpConn.setRequestProperty("Content-Length",
					String.valueOf(parament.toString().getBytes().length));
			httpConn.getOutputStream().write(parament.toString().getBytes());
			httpConn.getOutputStream().flush();
			httpConn.getOutputStream().close();
			InputStream is = httpConn.getInputStream();
			int nRead;
			byte[] bBuffer = new byte[1024];
			do {
				nRead = is.read(bBuffer);
				if (nRead <= 0)
					break;
				sb.append(new String(bBuffer, 0, nRead, "utf-8"));
			} while (true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

}
